package com.xthink.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xthink.common.result.AjaxResult;
import com.xthink.common.pojo.polo.PoolGroup;
import com.xthink.common.vo.pool.InsertGroupVo;
import com.xthink.common.vo.pool.ListGroupVo;
import com.xthink.web.mapper.PoolGroupMapper;
import com.xthink.web.service.PoolGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class PoolGroupServiceImpl extends ServiceImpl<PoolGroupMapper, PoolGroup> implements PoolGroupService {

    @Autowired
    private PoolGroupMapper poolGroupMapper;

    //小组下拉框
    @Override
    public AjaxResult selectGroup(Integer id) {
        LambdaQueryWrapper<PoolGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PoolGroup::getDelFlag,0);
        wrapper.eq(PoolGroup::getPoolId,id);
        List<PoolGroup> poolGroups = poolGroupMapper.selectList(wrapper);

        return AjaxResult.success(poolGroups);
    }

    //流量池内部添加小组名
    @Override
    public AjaxResult insertGroup(InsertGroupVo insertGroupVo) {
        String[] groupName = insertGroupVo.getGroupName();
        //判断小组名是否重复
        for (String s : groupName) {
            if (!StringUtils.isNotBlank(s)){
                return AjaxResult.error("请输入小组名称");
            }
            LambdaQueryWrapper<PoolGroup> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PoolGroup::getGroupName, s);
            Integer integer = poolGroupMapper.selectCount(wrapper);
            if (integer != 0) {
                return AjaxResult.error(s + "小组名重复,请重新输入小组名");
            }
        }

        for (String s : groupName) {
            PoolGroup poolGroup = new PoolGroup();
            poolGroup.setPoolId(insertGroupVo.getId());
            poolGroup.setGroupName(s);
            int insert = poolGroupMapper.insert(poolGroup);
            if (insert==0){
                return AjaxResult.error();
            }
        }
        return AjaxResult.success();
    }

    //查询流量池内部小组名称
    @Override
    public AjaxResult listGroup(ListGroupVo listGroupVo) {
        Page<PoolGroup> page = new Page<>(listGroupVo.getPage(), listGroupVo.getSize());
        LambdaQueryWrapper<PoolGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PoolGroup::getPoolId,listGroupVo.getPoolId());
        wrapper.eq(StringUtils.isNotBlank(listGroupVo.getGroupName()),PoolGroup::getGroupName,listGroupVo.getGroupName());
        wrapper.eq(PoolGroup::getDelFlag,0);
        Page<PoolGroup> poolGroupPage = poolGroupMapper.selectPage(page, wrapper);
        List<PoolGroup> records = poolGroupPage.getRecords();
        if (records.size()==0){
            return AjaxResult.error("未找到该小组或暂未添加小组");
        }
        return AjaxResult.success(records);
    }

    //流量池内部小组删除
    @Override
    public AjaxResult deleteGroup(Integer id) {

        PoolGroup poolGroup = poolGroupMapper.selectById(id);
        Integer cNumber = poolGroup.getCNumber();
        if (cNumber!=0){
            return AjaxResult.error("组内还有卡片，请先进行调拨");
        }

        LambdaUpdateWrapper<PoolGroup> poolGroupLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        poolGroupLambdaUpdateWrapper.eq(PoolGroup::getId,id).set(PoolGroup::getDelFlag,1);
        int update = poolGroupMapper.update(null, poolGroupLambdaUpdateWrapper);
        if (update==0){
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success();
    }
}
